package com.iwith.dongmoon.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.iwith.dongmoon.gsonlist.BoardList;
import com.iwith.dongmoon.gsonlist.BoardListItem;
import com.iwith.dongmoon.listadpater.FourTextListAdapter;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.SessionManager;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class Tab_NewsBoardActivity extends Activity implements OnClickListener{

	private SessionManager mSessionManager=null;
	
	private String userid;
	private Context mContext;
	
	private ListView boardList;
	private Button btn_write;
	
	private String graduation;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    // TODO Auto-generated method stub
	    setContentView(R.layout.tab_newsboard);
	    mSessionManager=new SessionManager(this);
	}
	
	private void Initialize()
	{
		mContext = this;
		userid = mSessionManager.getUserid();
		
		boardList = (ListView) findViewById(R.id.tabnewsboard_boardlist);
		btn_write = (Button) findViewById(R.id.tabnewsboard_btn_write);
			
		setBoardListData(ApiCode.NEWS, boardList);
		
		btn_write.setOnClickListener(this);
	}
	
	@Override
	public void onResume(){
		super.onResume();
		ThreadTask.onFinishAll();
		Initialize();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		ThreadTask.onFinishAll();
	}

	private void setBoardListData(final String code, final ListView listview) {
		ThreadInterface threadInterface = new ThreadInterface() {
			BoardList boardList;
			RequestGson requestGson = new RequestGson();
			public void onThread() {
				// TODO Auto-generated method stub
				String api = null;
				if(code.equals(ApiCode.NEWS) == true)
					api = ApiCode.API_NEWS;
				
				boardList = requestGson.getBoardGsonBySendingAPI(mContext, api,	"GET", "");
			}

			public void onThreadEnd() {
				// TODO Auto-generated method stub

				if (boardList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					ListAdapter listAdapter = new FourTextListAdapter(mContext, R.layout.listitem_fourtext,	(ArrayList<BoardListItem>) boardList.getList());
					listview.setAdapter(listAdapter); 
				}
				else {
					requestGson.alertToast(mContext, code+" 데이터 로딩 실패");
				}

			}
		};

		ThreadTask task = new ThreadTask((Activity)mContext, threadInterface);
		task.execute();
	}
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==btn_write.getId()){
			// TODO Auto-generated method stub
			Intent intent = new Intent(Tab_NewsBoardActivity.this, WriteActivity.class);
			intent.putExtra("userid", userid);
			intent.putExtra("code", ApiCode.NEWS);
			startActivity(intent);
		}
	}
	
}
